
var fs = require('fs');
var url = require("url")
var qs = require('querystring');


function route(handle, req_url, response, postData){
	
  var pathname = url.parse(req_url).pathname
  var rootPath = "client/publish/html5/"  //在dbl路径下执行node
  if(typeof handle[pathname]==='function')
  {
    var queryUrl = url.parse(req_url).query 
    var query = qs.parse(queryUrl)
    handle[pathname](response, postData, query)
  }
  else if(fs.existsSync(rootPath + pathname))
  {	

    fs.readFile(rootPath + pathname, function (err, data) { 
        if (err) return console.error(err);
        response.write(data)
        response.end()
    })

  }
  else
  {
    console.log("No request handler found for "+ pathname)
    response.writeHead(404,{"Content-Type":"text/plain"})
    response.write("404 Not found")
    response.end()
  }
}

exports.route = route